package com.gpframework.module.controller.wages;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.utils.poi.ExcelUtil;
import com.gpframework.module.wages.domain.GzOfficeSalary;
import com.gpframework.module.wages.domain.req.GzOfficeSalaryAdd;
import com.gpframework.module.wages.domain.req.GzOfficeSalaryReq;
import com.gpframework.module.wages.domain.req.GzOfficeSalaryUpdate;
import com.gpframework.module.wages.domain.vo.GzOfficeSalaryVo;
import com.gpframework.module.wages.service.IGzOfficeSalaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
/**
 * @ClassName GzOfficeSalaryController
 * @Author gpframework
 * @Date 2024-01-22
 * @Version 1.0.0
 * @Description 档口及办公室工资Controller
 */
@Validated
@Api(tags = "档口及办公室工资")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/wages/OfficeSalary" )
public class GzOfficeSalaryController extends BaseController {

    private final IGzOfficeSalaryService iGzOfficeSalaryService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2024-01-22
  * @Param gzOfficeSalary
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('wages:OfficeSalary:list')")
    @GetMapping("/list")
    public TableDataInfo<GzOfficeSalaryVo> list(GzOfficeSalaryReq gzOfficeSalaryReq)
    {
        startPage();
        QueryWrapper<GzOfficeSalary> lqw = new QueryWrapper<GzOfficeSalary>();
        gzOfficeSalaryReq.generatorQuery(lqw,true);
        List<GzOfficeSalaryVo> list = iGzOfficeSalaryService.listVo(lqw);
        return getDataTable(list);
    }


    @ApiOperation("统计列表")
    @PreAuthorize("@ss.hasPermi('wages:OfficeSalary:list')")
    @GetMapping("/listStatistics")
    public TableDataInfo<GzOfficeSalaryVo> listStatistics(GzOfficeSalaryReq gzOfficeSalaryReq)
    {
        startPage();
        QueryWrapper<GzOfficeSalary> lqw = new QueryWrapper<GzOfficeSalary>();
        gzOfficeSalaryReq.generatorQuery(lqw,true);
        List<GzOfficeSalaryVo> list = iGzOfficeSalaryService.listStatistics(lqw);
        return getDataTable(list);
    }



    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2024-01-22
     * @Param gzOfficeSalary
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('wages:OfficeSalary:add')" )
    @Log(title = "添加档口及办公室工资" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GzOfficeSalaryAdd gzOfficeSalaryAdd) {
        GzOfficeSalary gzOfficeSalary = Convert.convert(new TypeReference<GzOfficeSalary>() {}, gzOfficeSalaryAdd);
        return toAjax(iGzOfficeSalaryService.save(gzOfficeSalary) ? 1 : 0);
    }

    @ApiOperation("是否上报审批")
    @PreAuthorize("@ss.hasPermi('wages:OfficeSalary:edit')" )
    @Log(title = "是否上报审批" , businessType = BusinessType.UPDATE)
    @PutMapping("/peported")
    public AjaxResult peported(@Valid @RequestBody GzOfficeSalaryUpdate gzOfficeSalaryUpdate) {
        List<GzOfficeSalary> gzOfficeSalaryList = gzOfficeSalaryUpdate.getGzOfficeSalaryList();
        for (GzOfficeSalary gzOfficeSalary : gzOfficeSalaryList) {
            gzOfficeSalary.setEscalation(Long.valueOf("1"));
            iGzOfficeSalaryService.updateById(gzOfficeSalary);
        }
        return AjaxResult.success();
    }


    @ApiOperation("审批修改")
    @PreAuthorize("@ss.hasPermi('wages:OfficeSalary:edit')" )
    @Log(title = "审批修改档口及办公室工资" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody GzOfficeSalaryUpdate gzOfficeSalaryUpdates){
        for (GzOfficeSalary gzOfficeSalary : gzOfficeSalaryUpdates.getGzOfficeSalaryList()) {
            if ((Long.valueOf("1")).equals(gzOfficeSalary.getEscalation())){
                GzOfficeSalary gzOfficeSalaryUpdate = Convert.convert(new TypeReference<GzOfficeSalary>() {}, gzOfficeSalary);
                iGzOfficeSalaryService.updateById(gzOfficeSalaryUpdate);
            }else {
                return AjaxResult.error("请选择已经审核的人员");
            }
        }

        return AjaxResult.success();
    }




    @ApiOperation("批量修改")
    @PreAuthorize("@ss.hasPermi('wages:OfficeSalary:updateByIds')" )
    @Log(title = "批量修改档口及办公室工资" , businessType = BusinessType.UPDATE)
    @RequestMapping
    public AjaxResult updateByIds(@Valid @RequestBody List<GzOfficeSalaryUpdate> gzOfficeSalaryUpdates) {
        List<GzOfficeSalary> gzOfficeSalarys = Convert.toList(GzOfficeSalary.class, gzOfficeSalaryUpdates);
        return toAjax(iGzOfficeSalaryService.saveOrUpdateBatch(gzOfficeSalarys) ? 1 : 0);
    }
    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2024-01-22
     * @Param gzOfficeSalary
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('wages:OfficeSalary:export')" )
    @Log(title = "导出档口及办公室工资" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(GzOfficeSalaryReq gzOfficeSalaryReq) {
        QueryWrapper<GzOfficeSalary> lqw = new QueryWrapper<GzOfficeSalary>();
        gzOfficeSalaryReq.generatorQuery(lqw,true);
        List<GzOfficeSalaryVo> list = iGzOfficeSalaryService.listVo(lqw);
        ExcelUtil<GzOfficeSalaryVo> util = new ExcelUtil<GzOfficeSalaryVo>(GzOfficeSalaryVo. class);
        return util.exportExcel(list, "OfficeSalary" );
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2024-01-22
     * @Param gzOfficeSalary
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('wages:OfficeSalary:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GzOfficeSalaryVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGzOfficeSalaryService.getVoById(id));
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2024-01-22
     * @Param gzOfficeSalary
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('wages:OfficeSalary:remove')" )
    @Log(title = "删除档口及办公室工资" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGzOfficeSalaryService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }




}
